package com.example.java_second_hand_market;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.sql.*;
import java.util.List;

public class UserAdminDialog extends Stage {
    BorderPane bp;
    String DBUrl=ParameterDB.ParaDBUserUrl;
    String DBUser="root", DBPassword="123456";
    private final int ROWS_PER_PAGE = 10;
    @FXML
    private TableView<User> tableView = new TableView<>();
    @FXML
    private Pagination pagination;
    private ObservableList<User> data=FXCollections.observableArrayList();

    @FXML
    private Button ban=new Button("封禁");
    Admin ad;
    User it;
    public UserAdminDialog(Admin adm, int width,int height){
        ad=adm;
        data = getInitialData();

        // 配置TableView
        setupTableView();

        // 创建分页控件
        pagination = new Pagination((data.size() + ROWS_PER_PAGE - 1) / ROWS_PER_PAGE);
        pagination.setPageFactory(this::createPage);

        VBox vbox = new VBox(tableView, pagination);
        HBox hb=new HBox();
        hb.getChildren().addAll(ban);
        hb.setAlignment(Pos.CENTER);

        bp=new BorderPane();
        bp.setBottom(hb);
        bp.setCenter(vbox);

        Scene scene=new Scene(bp);
        this.setScene(scene);

        ban.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                banaction();
            }
        });

    }
    public ObservableList<User> getInitialData() {
        try{
            Connection con= DriverManager.getConnection(DBUrl,DBUser,DBPassword);
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery("SELECT * FROM user_list");
            while(rs.next()){
                User us=new User(rs.getString("name"),rs.getString("account"),rs.getString("password"));
                if(rs.getInt("is_ban")==1){
                    us.setIsban(true);
                }
                data.add(us);
            }
            if(con!=null) {
                try {
                    rs.close();
                    stmt.close();
                    con.close();

                }catch (SQLException err){
                    System.out.println("16767");
                }
            }
        }catch (SQLException e){
            System.out.println("暂无该用户数据"+e.getMessage());
        }
        return data;
    }
    private void setupTableView() {
        TableColumn<User, String> nameColumn = new TableColumn<>("姓名");
        nameColumn.setCellValueFactory(celldata ->{
            return new SimpleStringProperty(celldata.getValue().getName());
        });

        TableColumn<User, String> accountColumn = new TableColumn<>("用户名");
        accountColumn.setCellValueFactory(celldata ->{
            return new SimpleStringProperty(celldata.getValue().getAccount());
        });


        TableColumn<User,Boolean> banColumn = new TableColumn<>("是否被封禁");
        banColumn.setCellValueFactory(celldata ->{
            return new SimpleBooleanProperty(celldata.getValue().getIsban());
        });
        tableView.getColumns().addAll(nameColumn, accountColumn,banColumn);
    }
    private VBox createPage(int pageIndex) {
        int fromIndex = pageIndex * ROWS_PER_PAGE;
        int toIndex = Math.min(fromIndex + ROWS_PER_PAGE, data.size());

        List<User> pageItems = data.subList(fromIndex, toIndex);
        ObservableList<User> pageData = FXCollections.observableArrayList(pageItems);
        tableView.setItems(pageData);

        return new VBox(tableView);
    }
    public void banaction(){
        it=tableView.getSelectionModel().getSelectedItem();
        try {
            Connection con= DriverManager.getConnection(DBUrl,DBUser,DBPassword);
            Statement stmt=con.createStatement();
            stmt.executeUpdate("UPDATE user_list SET is_ban = 1 where name = '"+it.getName()+"'");
            if(con!=null){
                try {
                    stmt.close();
                    con.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        } catch (SQLException e) {
            System.out.println("1565");
        }
        data.clear();
        data = getInitialData();

        // 配置TableView


        // 创建分页控件
        pagination .setPageCount((data.size() + ROWS_PER_PAGE - 1) / ROWS_PER_PAGE);
        pagination.setPageFactory(this::createPage);

        tableView.setItems(data); // 直接设置TableView的数据源
        tableView.refresh();
        VBox vbox = new VBox(tableView, pagination);
        HBox hb=new HBox();
        hb.getChildren().addAll(ban);
        hb.setAlignment(Pos.CENTER);


        bp.setBottom(hb);
        bp.setCenter(vbox);

    }
    public BorderPane getbp(){
        return bp;
    }
}

